﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Company.Merchandising.DataAccessLayer.DataAccessCommon;
using WebApplication.Code;
using Telerik.Web.UI;
using WebApplication.Controls.Management.Dictionaries;
using System.Drawing;
using Company.Merchandising.ServiceLayer.EntityManagement;
using Company.Merchandising.BusinessLayer.DataTransferObjects;

namespace WebApplication.Restricted.Management.Dictionaries
{
	public partial class ChainStoresManagement : EntityPage
	{
		protected void ChainStoresGrid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
		{
			ChainStoresGrid.DataSource = new DACChainStore(ModelEntities).GetAllChainStores();
		}

		protected void ChainStoresGrid_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
		{
			GridEditableItem editedItem = e.Item as GridEditableItem;
			if (editedItem == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				return;
			}

			if (editedItem["Id"] == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				e.Canceled = true;
				return;
			}

			int editedChainStoreId;
			if (!int.TryParse(editedItem["Id"].Text, out editedChainStoreId))
			{
				AddErrorMessage("Bląd wewnętrzny");
				e.Canceled = true;
				return;
			}

			ChainStoresEditControl chainStoresControl = editedItem.FindControl(GridEditFormItem.EditFormUserControlID) as ChainStoresEditControl;
			if (chainStoresControl == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				return;
			}
			DTOChainStore dtoProvince = chainStoresControl.EditedChainStore;
			dtoProvince.Id = editedChainStoreId;
			ChainStoreManager chainStoreManager = new ChainStoreManager(ModelEntities);
			try
			{
				chainStoreManager.UpdateChainStore(dtoProvince);
			}
			catch (Exception ex)
			{
				AddErrorMessage(ex.Message);
				e.Canceled = true;
			}
		}

		protected void ChainStoresGrid_DeleteCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
		{
			GridEditableItem editedItem = e.Item as GridEditableItem;
			if (editedItem == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				e.Canceled = true;
				return;
			}

			if (editedItem["Id"] == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				e.Canceled = true;
				return;
			}

			int editedChainStoreId;
			if (!int.TryParse(editedItem["Id"].Text, out editedChainStoreId))
			{
				AddErrorMessage("Bląd wewnętrzny");
				e.Canceled = true;
				return;
			}

			ChainStoreManager chainStoreManager = new ChainStoreManager(ModelEntities);
			try
			{
				chainStoreManager.DeleteChainStore(editedChainStoreId);
			}
			catch (Exception ex)
			{
				AddErrorMessage(ex.Message);
				e.Canceled = true;
			}
		}

		protected void ChainStoresGrid_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
		{
			GridEditableItem editedItem = e.Item as GridEditableItem;
			if (editedItem == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				return;
			}

			ChainStoresEditControl provincesControl = editedItem.FindControl(GridEditFormItem.EditFormUserControlID) as ChainStoresEditControl;
			if (provincesControl == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				return;
			}

			ChainStoreManager chainStoreManager = new ChainStoreManager(ModelEntities);
			try
			{
				chainStoreManager.AddChainStore(provincesControl.EditedChainStore);
			}
			catch (Exception ex)
			{
				AddErrorMessage(ex.Message);
				e.Canceled = true;
			}
		}

		private void AddErrorMessage(string errorMessage)
		{
			Label errorLabel = new Label();
			errorLabel.Text = errorMessage;
			errorLabel.ForeColor = Color.Red;

			ChainStoresGrid.Controls.Add(errorLabel);
		}
	}
}
